package com.jtcl.netty.config;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.string.StringEncoder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

/**
 * Netty 客户端连接通道参数配置
 *
 * @author 施锦庭
 * @since 2021-02-24 9:46
 **/
@Component
@Slf4j
public class ChildChannelHandler extends ChannelInitializer<SocketChannel> {

    @Autowired
    private ServerHandler serverHandler;

    @Override
    public void initChannel(SocketChannel socketChannel) throws Exception {
        log.info("新的客户端进入IP：{},Port：{}", socketChannel.remoteAddress().getHostName(),
                socketChannel.remoteAddress().getPort());
        socketChannel.pipeline().addLast(new StringEncoder(StandardCharsets.UTF_8));
        socketChannel.pipeline().addLast(new OriginalStringDecoder());
        socketChannel.pipeline().addLast(serverHandler);
//        socketChannel.pipeline().addLast(new ByteArrayEncoder());
    }
}